const app = getApp();
const req = require("../../utils/http.js");
Page({

 
  data: {
    appid:'',
    publicAccountLoginUrl:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)

    let t = this
    let productId = options.productId
    let openid = wx.getStorageSync('openid')
    let obj = options.obj
    let appid = options.appid;
    let from = options.setfrom
    let baseUrl = app.globalData.baseUrl
    let address = ''
    let url = ''
    let reqHeader =''
    wx.getStorage({
      key: 'address',
      success: function (res) {
        if (res) {
          console.log(res)
          address = res.data

        } else {
          console.log('没有信息')
        }
      },
    })
    if (obj =='receiveCoupon'){
      url = '/coupon/wxProduct/receiveCoupon' //优惠卷
      reqHeader = "GET"
    }else{
      url = '/coupon/wxProduct/wxPay' //商品
      reqHeader = "POST"
    }
   
    t.setData({
      appid: wx.getAccountInfoSync().miniProgram.appId,
      openid,
      publicAccountLoginUrl: 'http://wechat.alipayjf.com/api/open/authorize?state=' + openid + '&appid=' + appid + '&from=' + from
    })
    let interval = setInterval(function () { isAuth(productId) }, 1500);
    let isAuth = function (productId){
      let params = { productId, address }
      req.wxReq('/coupon/wxProduct/wxPay', reqHeader, params).then(res => {
        if (res.data.code == 200) {
          clearInterval(interval);
          wx.navigateBack({
            delta: 1
          })
        } else{
          setTimeout(()=>{
            clearInterval(interval);
          },15000)
        }

      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})